package org.apache.lorry.zookeeper.cluster;

import java.util.*;

public class ChildCache {
    private Set<String> childSet = new HashSet<>();


    public List<String> getChildList() {
        return Collections.unmodifiableList(new ArrayList<>(childSet));
    }

    public ChildCache() {
    }


    public ChildCache(List<String> childSet) {
        this.childSet = new HashSet<>(childSet);
    }


    public synchronized List<String> addChildDiff(List<String> remoteChildList) {
        List<String> newChildList = new ArrayList<>();
        for (String child : remoteChildList) {
            if (!childSet.contains(child)) {
                childSet.add(child);
                newChildList.add(child);
            }
        }
        this.childSet = new HashSet<>(remoteChildList);
        return newChildList;
    }


    public synchronized List<String> removeChildDiff(List<String> remoteChildList) {
        List<String> removeChildList = new ArrayList<>();
        for (String child : childSet) {
            if (!remoteChildList.contains(child)) {
                removeChildList.add(child);
            }
        }
        this.childSet = new HashSet<>(remoteChildList);
        return removeChildList;
    }
}